function project_new_show()
{
    if ($('form_project').currentStyle.display == 'block') {
        project_new_cancel();
        return;
    }
	project_currency_options(default_currency);
    project_form_show('form_project');
	var idate = $('project_new_date');
	if (!idate.value.length) {
		idate.value = date_toDay();
	}
    $('project_new_name').focus();
}
function project_new_cancel()
{
    project_form_hide();
}
function project_new()
{
    var idate = $('project_new_date');
    var iname = $('project_new_name');
    var irate = $('project_new_rate');
	var icurrency = $('project_new_currency');
    var date = idate.value.trim();
    var name = iname.value.trim();
    var rate = irate.value.trim();
	var currency = icurrency.value;
    if (!date) date = date_toDay();
    if (!name) {
        alert(lang.project_name_empty);
        return false;
    }
    if (!rate) rate = '';
    if (rate) {
        var ra = rate.replace(/,/g, '.');
        if (!ra.match(/^\d+(\.\d+)?$/)) {
            alert(lang.invalid_rate);
            return false;
        }
        rate = Number(ra);
    }
    var new_id = ++projects.seq_id;
    projects.rows[new_id] = {
        'id': new_id,
        'date': escapeTags(date),
        'name': escapeTags(name),
        'rate': rate,
		'currency': currency
    };
    project_save();
    task_save_new(new_id);
    idate.value = '';
    iname.value = '';
    irate.value = '';
	project_currency_options(default_currency);
    project_list();
    project_form_hide();
}
function project_list()
{
    var html = '<table class="ls" cellspacing="1" cellpadding="0"><tr><th width="14%" align="center">'+lang.date+'</th><th>'+lang.project+'</th><th width="10%">'+lang.rate+'</th></tr>';
    var rows = projects_rows_latest();
    for (var i = 0; i < rows.length; i++)
    {
        var proj = rows[i];
		var rate = proj.rate;
		if (rate) {
			var curr = rows[i].currency;
			if (!curr) {
				curr = default_currency;
			}
			rate += ' '+curr;
		} else {
			rate = '-';
		}
        html += ('<tr><td align="center">'+proj.date+'</td><td id="project_name_'+proj.id+'"><span onclick="project_edit_show('+proj.id+')" style="cursor: pointer;">'+proj.name+'</span></td><td align="center">'+rate+'</td></tr>').replace(/<td/g, '<td style="background: #eee;"');
    }
    html += '</table>';
    $('projects_div').innerHTML = html;
}
var project_edit_id = -1;
function project_edit_show(id)
{
    if (project_edit_id == id) {
        project_edit_cancel();
        return;
    }
    project_form_show('form_project');
    project_edit_id = id;
    $('project_new_button').style.display = 'none';
    $('project_edit_span').style.display = '';
    var idate = $('project_new_date');
    var iname = $('project_new_name');
    var irate = $('project_new_rate');
    var project = projects.rows[id];
    idate.value = project.date;
    iname.value = project.name;
    irate.value = project.rate;
	project_currency_options(project.currency);
    $('project_new_name').focus();
}
function project_edit_cancel()
{
    if (project_edit_id == -1) {
        return;
    }
    project_edit_id = -1;
    $('project_new_button').style.display = '';
    $('project_edit_span').style.display = 'none';
    var idate = $('project_new_date');
    var iname = $('project_new_name');
    var irate = $('project_new_rate');
    idate.value = '';
    iname.value = '';
    irate.value = '';
	project_currency_options(default_currency);
    project_form_hide();
}
function project_edit()
{
    var idate = $('project_new_date');
    var iname = $('project_new_name');
    var irate = $('project_new_rate');
	var icurrency = $('project_new_currency');
    var date = idate.value.trim();
    var name = iname.value.trim();
    var rate = irate.value.trim();
	var currency = icurrency.value;
    if (!date) date = date_toDay();
    if (!name) {
        alert(lang.project_name_empty);
        return false;
    }
    if (!rate || 'null' == rate) rate = '';
    if (rate) {
        var ra = rate.replace(/,/g, '.');
        if (!ra.match(/^\d+(\.\d+)?$/)) {
            alert(lang.invalid_rate);
            return false;
        }
        rate = Number(ra);
    }
    var id = project_edit_id;
    projects.rows[id].date = escapeTags(date);
    projects.rows[id].name = escapeTags(name);
    projects.rows[id].rate = rate;
	projects.rows[id].currency = currency;
    project_save();
    project_list();
    project_edit_cancel();
    project_form_hide();
}
function curr_currency()
{
	if (curr_project.currency) {
		return curr_project.currency;
	} else {
		return default_currency;
	}
}
function project_delete()
{
    if (projects_rows().length == 1) {
        alert(lang.only_one_project);
        return false;
    }
    if (confirm(lang.project_delete_confirm)) {
        project_remove(project_edit_id);
        project_save();
        project_list();
        project_edit_cancel();
        project_form_hide();
    }
}
function project_remove(id)
{
    var ret = {};
    for (var i in projects.rows) {
        if (id == i) continue;
        ret[i] = projects.rows[i];
    }
    projects.rows = ret;
    if (projects.selected == id) {
        project_load(maxProjectId(), true);
    }
}
function project_form_show(id)
{
    project_edit_cancel();
    $('form_project').style.display = 'block';
}
function project_form_hide()
{
    $('form_project').style.display = 'none';
}
function project_refresh()
{
    project_new_cancel();
    project_edit_cancel();
    project_list();
}
function project_currency_options(sel)
{
	var ret = '<select id="project_new_currency"><option value=""></option>';
	for (var i = 0; i < currencies.length; i++) {
		var curr = currencies[i];
		if (curr == sel) {
			ret += '<option value="'+curr+'" selected="selected">'+curr+'</option>';
		} else {
			ret += '<option value="'+curr+'">'+curr+'</option>';
		}
	}
	ret += '</select>';
	$('project_new_currency_wrapper').innerHTML = ret;
}